#include "Network.h"

ServerSocket::ServerSocket() {
    memset(&ServerAddress,0,sizeof(ServerAddress));
    memset(&ClientAddress,0,sizeof(ClientAddress));
    IPAddress = "127.0.0.1";
    Port = 8086;
    SendMessage = new char[256];
    ReceiveMessage = new char[256];
}

char * ServerSocket::showReceiveMessage() {
    return ReceiveMessage;
}

void ServerSocket::SetIPAddress(char* IPAddress) {
    this->IPAddress = IPAddress;
}

void ServerSocket::SetPort(int Port) {
    this->Port = Port;
}

void ServerSocket::SetSocket() {
    ServerSock = socket(AF_INET,SOCK_STREAM,0);
    ServerAddress.sin_family = AF_INET;
    ServerAddress.sin_addr.s_addr = inet_addr(IPAddress);
    ServerAddress.sin_port = htons(Port);
    socklen_t ClientAddressSize = sizeof(ClientAddress);
    ClientSock = accept(ServerSock,(sockaddr*)&ClientAddress,&ClientAddressSize);
}

void ServerSocket::SetSendMessage(string SendMessage) {
    this->SendMessage = SendMessage;
}

bool ServerSocket::Receive() {
    bind(ServerSock,(sockaddr*)&ServerAddress,sizeof(sockaddr));
    listen(ServerSock,20);
    char str[256];
    read(ClientSock,str,sizeof(str));
    ReceiveMessage = CharArrayToString(str,sizeof(str));
    return true;
}

bool ServerSocket::Send() {
    char str[] = SendMessage;
    write(ClientSock,str,sizeof(str));
    return true;
}